package cs304.p3.library.framework;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class LibraryDataAccessContext {

	/*
	 * This are set to non-final because we can each login with out own account
	 * More modification will be needed but this is not a proity.
	 * 
	 * Connection strings are inside MainFrame since they can be selected via
	 * home/ubc
	 */
	public static final String connectionStrings[] = {"jdbc:oracle:thin:@dbhost.ugrad.cs.ubc.ca:1522:ug", "jdbc:oracle:thin:@localhost:1522:ug"};
	public static final String DEFAULT_JDBCURL = connectionStrings[0]; // "jdbc:oracle:thin:@dbhost.ugrad.cs.ubc.ca:1522:ug";
	public static final String DEFAULT_USERNAME = "ora_c8v6";
	public static final String DEFAULT_PASSWORD = "a66595083";
	
	public static String jdbcUrl = DEFAULT_JDBCURL;
	public static String username = DEFAULT_USERNAME;
	public static String password = DEFAULT_PASSWORD;
	
	
	

	private LibraryDataAccessContext() {
	}

	public static Connection initConnection() {
		Connection con = null;
		try {
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			con = DriverManager.getConnection(DEFAULT_JDBCURL,
					DEFAULT_USERNAME, DEFAULT_PASSWORD);
		} catch (SQLException ex) {
			ex.printStackTrace();
		}

		return con;
	}

	// Dear Oscar,
	//
	// Some changes happened here. I moved all our single instance objects to
	// IoC which will manage the objects and dependency injections so
	// this includes the Connection object that used to belong to this class.
	// This makes more sense to me now (and hopefully for you too), because
	// LibrarayDataAccessContext should only manage the connection type,
	// address, user/password information (context) but not the actual
	// connection object itself.

	// Also, please try to keep gui code strictly in gui classes. You can update
	// the views based on exceptions thrown and by accessing context by the
	// static fields eg. LibraryDataAccessContext.username (the username should
	// be updated every time you switch logins).
	//
	//							Sincerely,
	//									Pam

	public static Connection reconnect(){
		closeConnection(IoC.getConnection());
		Connection con = null;
		try {
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			con = DriverManager.getConnection(jdbcUrl, username, password);
		} catch (SQLException ex) {
			ex.printStackTrace();
			try {
				DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
				con = DriverManager.getConnection(DEFAULT_JDBCURL, DEFAULT_USERNAME, DEFAULT_PASSWORD);
			} catch (SQLException ex2) {
				ex2.printStackTrace();
			}
			System.out.println("FALL BACK TO DEFAULT CONNECTION");
			jdbcUrl = DEFAULT_JDBCURL;
			username = DEFAULT_USERNAME;
			password = DEFAULT_PASSWORD;
			//System.out.println("Logging in with: " + username + "\n" + password +"\n" + jdbcUrl);
		}
		if(con != null) System.out.println("New Connection Established");
		return con;
	}

	public static void closeConnection(Connection connection) {
		if (connection == null){
			System.err.println("Attempt to close NULL connection");
			return;
		}
		try {
			connection.close();
		} catch (SQLException e) {
			System.err.println("Unable to close Connection");
			e.printStackTrace();
		}
		System.out.println("Connection Closed");
	}

}
